package cn.tt.tuantuanerp.tuantuanerp.warehouse.service.impl;


import cn.tt.tuantuanerp.tuantuanerp.comment.ex.ServiceException;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.WarehouseCheckDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.entity.WarehouseCheck;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.WarehouseCheckVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.utils.IdGenerator;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonPage;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.ServiceCode;
import cn.tt.tuantuanerp.tuantuanerp.product.service.ISkuService;
import cn.tt.tuantuanerp.tuantuanerp.warehouse.mapper.WarehouseCheckMapper;
import cn.tt.tuantuanerp.tuantuanerp.warehouse.service.IWarehouseCheckService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
@Slf4j
public class WarehouseCheckServiceImpl implements IWarehouseCheckService {

    @Autowired
    WarehouseCheckMapper warehouseCheckMapper;
    @Override
    public void addNew(WarehouseCheckDTO warehouseCheckDTO) {
        WarehouseCheck warehouseCheck = new WarehouseCheck();
        warehouseCheckDTO.setApprovalStatus((short)0);
        warehouseCheckDTO.setCheckTime(LocalDateTime.now());
        BeanUtils.copyProperties(warehouseCheckDTO,warehouseCheck);
        warehouseCheckMapper.insert(warehouseCheck);
    }

    @Override
    public void setCheckPass(Long checkWarehouseId) {
        log.debug("开始处理改变审核请求业务,请求ID为:{}",checkWarehouseId);
        WarehouseCheckVO standardById = warehouseCheckMapper.getStandardById(checkWarehouseId);
        if (standardById==null){
            log.debug("审核失败,未查询到id为:{}的盘点订单",checkWarehouseId);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"审核失败,未查询到"+checkWarehouseId+"盘点批次");
        }
        log.debug("已查询到id为{}的盘点批次",checkWarehouseId);
        if(standardById.getApprovalStatus()==1){
            log.debug("该批次已通过审核,无需再审核");
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"该批次已通过审核,无需再审核");
        }
        WarehouseCheck warehouseCheck = new WarehouseCheck();
        warehouseCheck.setCheckWarehouseId(checkWarehouseId);
        warehouseCheck.setApprovalStatus((short)1);
        int i = warehouseCheckMapper.updateById(warehouseCheck);
        if(i!=1){
            String message = "审批失败,服务器繁忙";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public Long returnID(LoginPrincipal loginPrincipal) {
        Long longDate = IdGenerator.getCheckId(loginPrincipal);
        log.debug("得到了随机生成的盘点号:{}",longDate);
        WarehouseCheckVO standardById = warehouseCheckMapper.getStandardById(longDate);
        if (standardById!=null){
            String message = "服务器繁忙";
            log.warn(message);
            throw new ServiceException(ServiceCode.Err_IS_EXIST,message);
        }
        return longDate;
    }

    @Override
    public JsonPage<WarehouseCheckVO> list(Integer startIndex, Integer pageSize) {
        PageHelper.startPage(startIndex, pageSize);
        List<WarehouseCheckVO> list = warehouseCheckMapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public WarehouseCheckVO getStandardById(Long id) {
        WarehouseCheckVO standardById = warehouseCheckMapper.getStandardById(id);
        return standardById;
    }

}
